package cn.edu.gsau.info.service.impl;

import cn.edu.gsau.info.dao.KeyanDao;
import cn.edu.gsau.info.models.Keyan;
import cn.edu.gsau.info.models.User;
import cn.edu.gsau.info.models.Zhengmingcailiao;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.IKeyanService;
import cn.edu.gsau.info.service.IZhengmingcailiaoService;
import cn.edu.gsau.info.vo.Params;
import cn.edu.gsau.info.vo.UploadFile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jinjiang
 * @since 2022-11-17
 */
@Service
public class KeyanServiceImpl extends ServiceImpl<KeyanDao, Keyan> implements IKeyanService {
    @Value("${gsau.upload.path}")
    private  String path;

    @Autowired
    private KeyanDao keyanDao;

    @Autowired
    IZhengmingcailiaoService zhengmingcailiaoDao;

    @Override
    public void addInfo(Keyan keyan) {
        keyan.setTianbaoId(4L);
        keyan.setStatus("进行中");
        keyan.setNode("审核中");
        keyan.setCreateTime(new Date());
        keyan.setDeleted(0);
        keyanDao.insert(keyan);

        Long id = keyan.getId();
        UploadFile[] fileList = keyan.getFileList();

        for (int i = 0; i < fileList.length; i++) {
            System.out.println(fileList[i].getNewFileName());
            Zhengmingcailiao zm = new Zhengmingcailiao();
            zm.setModuleId(ModuleIdClass.KEYAN_id);
            zm.setRecoreId(id);
            zm.setFileName(fileList[i].getOldFileName());
            zm.setFileFullPath("/101/"+fileList[i].getNewFileName());
            zhengmingcailiaoDao.save(zm);
        }
    }

    @Override
    public List<Keyan> selectAll() {
        return keyanDao.selectList(null);
    }

    @Override
    public List<Keyan> selectById(User user) {
        QueryWrapper<Keyan> qw = new QueryWrapper();
        qw.eq("id" , user.getId());
        List<Keyan> list = keyanDao.selectList(qw);
        return list;
    }

    @Override
    public List<Keyan> selecteByCondation(Params params) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("xiangmuName" , params.getXiangmuName());
        map.put("status",params.getStatus());
        map.put("tianbaoId",params.getTianbaoId());
        IPage<Keyan> iPage = new Page<Keyan>(params.getPageYeShu(), 5);
        QueryWrapper qw = new QueryWrapper();
        qw.like(map.get("xiangmuName") != null && !map.get("xiangmuName").equals(""),"xiangmuName",map.get("xiangmuName"));
        qw.eq(map.get("status") != null && !map.get("status").equals(""),"status",map.get("status"));
        qw.eq("tianbaoId",map.get("tianbaoId"));
        IPage page = keyanDao.selectPage(iPage,qw);
        List list = new ArrayList();
        list.add(page);
        return list;
    }

}
